package com.ruoyi.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.business.domain.FilePerson;
import com.ruoyi.business.domain.vo.FilePersonImportVo;
import com.ruoyi.business.listener.FilePersonImportListener;
import com.ruoyi.business.service.IFilePersonService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 人员管理Controller
 *
 * @author ruoyi
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/business/person")
public class FilePersonController extends BaseController {
    @Autowired
    private IFilePersonService filePersonService;


    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @SaCheckPermission("business:person:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<FilePersonImportVo> result = ExcelUtil.importExcel(file.getInputStream(), FilePersonImportVo.class, new FilePersonImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 查询人员管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:person:list')")
    @GetMapping("/list")
    public TableDataInfo list(FilePerson filePerson, PageQuery pageQuery) {

        return filePersonService.selectPageFilePersonList(filePerson, pageQuery);

//        startPage();
//        List<FilePerson> list = filePersonService.selectFilePersonList(filePerson);
//        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('business:person:list')")
    @GetMapping("/fromList")
    public TableDataInfo fromList(FilePerson filePerson, PageQuery pageQuery) {
        return filePersonService.selectPersonListByFromId(filePerson, pageQuery);
    }

    @PreAuthorize("@ss.hasPermi('business:person:list')")
    @GetMapping("/inList")
    public TableDataInfo inList(FilePerson filePerson, PageQuery pageQuery) {
        return filePersonService.selectPersonListByInId(filePerson, pageQuery);
    }

    /**
     * 查询人员管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:person:listFile')")
    @GetMapping("/listFile")
    public TableDataInfo listFile(FilePerson filePerson, PageQuery pageQuery) {
        return filePersonService.selectPageFilePersonList(filePerson, pageQuery);
//        startPage();
//        List<FilePerson> list = filePersonService.selectFilePersonList(filePerson);
//        return getDataTable(list);
    }


    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping("/persons")
    public R<List<FilePerson>> getUserList(String ids) {
        return R.ok(filePersonService.selectUserByIds(ids));
    }


    /**
     * 导出人员管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:person:export')")
    @Log(title = "人员管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(FilePerson filePerson, HttpServletResponse response) {
        List<FilePerson> list = filePersonService.selectFilePersonList(filePerson);
        ExcelUtil.exportExcel(list, "人员管理", FilePerson.class, response);
//        ExcelUtil<FilePerson> util = new ExcelUtil<FilePerson>(FilePerson.class);
//        return util.exportExcel(list, "person");
    }

    /**
     * 获取人员管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:person:query')")
    @GetMapping(value = "/{id}")
    public R<FilePerson> getInfo(@PathVariable("id") Long id) {
        return R.ok(filePersonService.selectFilePersonById(id));
    }

    /**
     * 新增人员管理
     */
    @PreAuthorize("@ss.hasPermi('business:person:add')")
    @Log(title = "人员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody FilePerson filePerson) {
        return toAjax(filePersonService.insertFilePerson(filePerson));
    }

    @PreAuthorize("@ss.hasPermi('business:person:add')")
    @Log(title = "人员管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RequestMapping("/from")
    public R addFromPerson(@RequestBody FilePerson filePerson) {
        return toAjax(filePersonService.insertFromFilePerson(filePerson));
    }

    @PreAuthorize("@ss.hasPermi('business:person:add')")
    @Log(title = "人员管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RequestMapping("/in")
    public R addInPerson(@RequestBody FilePerson filePerson) {
        return toAjax(filePersonService.insertInFilePerson(filePerson));
    }

    /**
     * 修改人员管理
     */
    @PreAuthorize("@ss.hasPermi('business:person:edit')")
    @Log(title = "人员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody FilePerson filePerson) {
        return toAjax(filePersonService.updateFilePerson(filePerson));
    }

    /**
     * 删除人员管理
     */
    @PreAuthorize("@ss.hasPermi('business:person:remove')")
    @Log(title = "人员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids) {
        return toAjax(filePersonService.deleteFilePersonByIds(ids));
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "人员数据", FilePersonImportVo.class, response);
    }

}
